package com.spider.silence.worker;

import com.spider.silence.abstarct.downloader.AbstractCommonDownLoader;
import com.spider.silence.downloader.TianFuForumDownLoader;
import com.spider.silence.main.ChengduLifeMain;
import com.spider.silence.main.Main;
import com.spider.silence.pool.TianFuForumDownLoaderPool;
import com.spider.silence.task.TianFuForumModuleTask;
import com.spider.silence.task.TianFuForumTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.CommonUtils;
import com.spider.silence.utils.PropertiesUtil;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import us.codecraft.webmagic.Page;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Administrator
 * @description 描述
 * @time 2017/2/6 0006
 */
public class TianFuForumModuleWorker extends AbstractCommonDownLoader implements Runnable{
    private Logger logger = Logger.getLogger(KaiDiModuleWorker.class);

    public String getDsign(String url) throws Exception {
        String no="";
        while (true){

            String aa = "";
            try {
                aa = getString(url);
            }catch(Exception e){
                aa = getString(url);
            }

            if(aa!=null && !"".equals(aa)) {
                aa = aa.substring(31, aa.length() - 9);
                aa = "var cc='';\n" +
                        "getName.caller={};\n" +
                        "var window=function () {};\n" +
                        "var location=function () {};\n" +
                        "window.prototype.href='';\n" +
                        "location.prototype.href=window.prototype.href;\n" +
                        "location.replace=function(str){ if(str.length>0){cc=str}else{cc=window.href;}};\n" +
                        "location.assign=function(str){ if(str.length>0){cc=str}else{cc=window.href;}};" + aa + "function test(){return cc;};test();";
                ScriptEngineManager manager = new ScriptEngineManager();
                ScriptEngine engine = manager.getEngineByName("javascript");
                try {
                    String cc = (String) engine.eval(aa);
                    if (!cc.isEmpty() && cc.split("=")[1].length() == 8) {
                        no = cc.split("=")[1];
                        break;
                    } else {
                        continue;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return no;
}

    @Override
    public void run() {

        while(true&&!Thread.interrupted()) {
            try {
                TianFuForumModuleTask moduleTask = (TianFuForumModuleTask) CommonTaskManager.takeTask("TianFuForumModule",1000);
                if(null!=moduleTask){
                    String module_url = moduleTask.getModuleUrl();
                    if("http://bbs.scol.com.cn/forum-2550-1.html".equals(module_url)){
                        Document document = get(module_url);
                        Elements imgList = document.getElementById("waterfall").select("li");
                        //String forum_url = urlList.get(i);

                        //elementsList.add(imgList);

                        for (int i = 0; i < imgList.size(); i++){
                            String forumUrl = imgList.get(i).select(".c").select("a").get(0).attr("href");

                            Page htmlPage = getHtml(forumUrl);
                            Document document_url = htmlPage.getHtml().getDocument();
                            //String title = document.title();
                            if (document.title() == null || "".equals(document.title())) {
                                String design = getDsign(forumUrl);
                                forumUrl = forumUrl + "?_dsign="+design;
                            }

                            TianFuForumTask task = new TianFuForumTask(URLDecoder.decode(forumUrl, "UTF-8"));
                            CommonTaskManager.add("TianFuForum", task);
                            System.out.println(forumUrl);


                        }


                       String total = document.select("#pgt").select("#fd_page_bottom").select(".pg").select("label").text();
                        Pattern pattern = Pattern.compile("[^0-9]");
                        Matcher matcher = pattern.matcher(total);
                        String all = matcher.replaceAll("");
                        System.out.println("page:" + all);
                        int total_page = Integer.valueOf(all);
//                        if(total_page>=2) {
//                            for (int j = 2; j <= total_page; j++) {
//                                try {
//                                    String new_url = module_url.split("-")[0] + "-" + module_url.split("-")[1] + "-" + j + ".html";
//                                    Document document1 = get(new_url);
//
//                                    Elements imgList1 = document1.getElementById("waterfall").select("li");
//                                    for (int i = 0; i < imgList.size(); i++) {
//                                        String forumUrl = imgList.get(i).select(".c").select("a").get(0).attr("href");
//
//                                        String design = getDsign(forumUrl);
//                                        forumUrl = forumUrl + "?_dsign=" + design;
//                                        TianFuForumTask task = new TianFuForumTask(URLDecoder.decode(forumUrl, "UTF-8"));
//                                        CommonTaskManager.add("TianFuForum", task);
//                                        System.out.println(forumUrl);
//                                    }
//                                } catch (Exception e) {
//                                    continue;
//                                }
//                            }
//                        }


                    }else{
                        //List<Elements> elementsList = new ArrayList<Elements>();
                        Document document = get(module_url);
                        Elements threadList = document.select("tbody[id^='normalthread_'] a.xst");
                        String forum_url = module_url;
                        for (int k = 0; k < threadList.size(); k++) {
                            //int index = taskIndex.get(k) - 1;
                            //String url = elements.get(index).attr("href");
                            try {
                                //Document document = get(url);
                                //Elements forumElements = document.select("tbody[id^='normalthread_'] a.xst");
                                String forumUrl = threadList.get(k).attr("href");
                                Page htmlPage = getHtml(forumUrl);
                                document = htmlPage.getHtml().getDocument();
                                //String title = document.title();
                                if (document.title() == null || "".equals(document.title())) {
                                    String design = getDsign(forumUrl);
                                    forumUrl = forumUrl + "?_dsign="+design;
                                }

                                TianFuForumTask task = new TianFuForumTask(URLDecoder.decode(forumUrl, "UTF-8"));
                                CommonTaskManager.add("TianFuForum", task);
                                System.out.println(forumUrl);

                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                                e.printStackTrace();
                            }
                        }
                        String total = document.select("#fd_page_bottom").select(".pg").select(".last").text();
                        Pattern pattern = Pattern.compile("[^0-9]");
                        Matcher matcher = pattern.matcher(total);
                        String all = matcher.replaceAll("");
                        System.out.println("page:" + all);
                        int total_page = Integer.valueOf(all);

//                        if(total_page>=2) {
//                            for (int k = 2;k<=total_page;k++) {
//                                Elements threadList1 = null;
//                                try {
//                                    String new_url = forum_url.split("-")[0] + "-" + forum_url.split("-")[1] + "-" + k + ".html";
//                                    //String new_url = forum_url.split("-")[0] + "-" + forum_url.split("-")[1] + "-" + k + ".html";
//                                    Document document1 = get(new_url);
//                                    threadList1 = document1.select("tbody[id^='normalthread_'] a.xst");
//
//                                    for(int j = 0;j<threadList1.size();j++) {
//                                        String forumUrl = threadList1.get(j).attr("href");
//                                        String design = getDsign(forumUrl);
//                                        forumUrl = forumUrl + "?_dsign=" + design;
//                                        TianFuForumTask task = new TianFuForumTask(URLDecoder.decode(forumUrl, "UTF-8"));
//                                        CommonTaskManager.add("TianFuForum", task);
//                                        System.out.println(forumUrl);
//                                    }
//
//                                } catch (Exception e) {
//                                    Logger logger = Logger.getLogger(Main.class);
//                                    logger.error("解析url出错", e);
//                                    continue;
//                                }
//                            }
//                        }
                    }

                }
            } catch (Exception e) {
                logger.error(e.getMessage(),e);
                e.printStackTrace();
            }
        }
    }

    /**
     * 爬取历史数据
     * @param urlList
     */
    public void generateAllTask(List<String> urlList){
        int a=0;
        for (int i = 0; i < urlList.size(); i++) {
            boolean flag = true;
            Document document = null;
            long startTime = System.currentTimeMillis();
            long nowTime = startTime;
            while(flag && nowTime - startTime < Long.valueOf(PropertiesUtil.getVal("TianFuForum.WaitTime"))) {
                try {
//                    String proxy=list.get(new Random().nextInt(list.size()))[2]+":"+list.get(new Random().nextInt(list.size()))[3];
                    Page pageContent = getHtml(urlList.get(i));
                    if (null == pageContent) {

                    } else if (pageContent.getStatusCode() == 200) {
                        flag=false;
                        document = pageContent.getHtml().getDocument();
                        int page = 0;
                        if (document.select("#fd_page_top").select(".pg").size() == 0) {
                            page = 1;
                        } else {
                            page = Integer.parseInt(document.select("#fd_page_top").select(".pg").select("label").select("span").get(0).attr("title").split(" ")[1]); //页码
                        }
                        for (int j = 1; j < page + 1; j++) {
                            try {
                                Document d = get(urlList.get(i).split("-")[0] + "-" + urlList.get(i).split("-")[1] + "-" + j + ".html");
                                if("http://bbs.scol.com.cn/forum-2550-1.html".equals(urlList.get(i))){  //天府旅游
                                    Elements element = d.select(".c.cl");
                                    if (element.size() > 0) {
                                        for (int k = 0; k < element.size(); k++) {
                                            String url = element.get(k).select("a").attr("href");
                                            Document doc=get(url);
                                            if (doc.title() == null || "".equals(doc.title())) {
                                                String design = getDsign(url);
                                                url = url + "?_dsign="+design;
                                            }
                                            TianFuForumTask task = new TianFuForumTask(URLDecoder.decode(url, "UTF-8"));
                                            CommonTaskManager.add("TianFuForum", task);
                                        }
                                    }
                                }else {
                                    Elements element = d.select(".s.xst");
                                    if (element.size() > 0) {
                                        for (int k = 0; k < element.size(); k++) {
                                            String url = element.get(k).attr("href");
                                            Document doc=get(url);
                                            if (doc.title() == null || "".equals(doc.title())) {
                                                String design = getDsign(url);
                                                url = url + "?_dsign="+design;
                                            }
                                            TianFuForumTask task = new TianFuForumTask(URLDecoder.decode(url, "UTF-8"));
                                            CommonTaskManager.add("TianFuForum", task);
                                        }
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                continue;
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Logger logger = Logger.getLogger(ChengduLifeMain.class);
                }finally {
                    nowTime = System.currentTimeMillis();
                }
            }


        }
    }
}
